// pages/search/search.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: "",
    screenHeight: 0,
    swiperHeight: 0,
    textVal: "",
    showWall: false,
    showHole: false,

    search_text_flag: false,

    wallData: [],
    holeData: [],

    show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['表白墙', '树洞'], //下拉列表的数据
    index: 0 //选择的下拉列表下标
  },

  handleTextInput: function (e) {
    var self = this;
    let value = e.detail.value;
    self.setData({
      textVal: value
    })
  },
  
  wallSearch: function () {
    var wallList = [];
    var self = this;
    var textVal = self.data.textVal;
    let listData = this.data.wallData
    var data = {
      pageIndex: 1,
      pageSize: 50,
      text: textVal
    }
    app.wxapi('POST', '/wall/content/search-wall', data, "application/x-www-form-urlencoded", (res) => {
      console.log(res);
      if (res.success) {
        if (res.data == null) {
          self.setData({
            text: "很遗憾，没找到"
          })
          return;
        }
        for (var i = 0; i < res.data.length; i++) {
          var wallData = res.data[i];
          wallList.push(wallData);
        }
        /*
         * 导入数据
         */
        listData = listData.concat(wallList)
        this.setData({
          wallData: listData,
          navState: 1,
          text: "找到啦！！！",
          showWall: true,
        })
      } else {
        this.setData({
          text: "很遗憾，没找到"
        })
        console.log("获取失败");
      }
    }, (err) => {
      //调用服务端登录接口失败
      console.log(err.errMsg)
    });
  },
  holeSearch: function () {
    var self = this;
    var holeList = [];
    var textVal = self.data.textVal;
    let listData = self.data.holeData;
    var data = {
      pageIndex: 0,
      pageSize: 500,
      keywords: textVal
    }
    app.wxapi('POST', "/hole/search", data, 'application/x-www-form-urlencoded', res => {
      console.log(res);
      if (res.success) {
        for (var i = 0; i < res.data.length; i++) {
          var holeData = {
            holeID: "",
            text: "",
            name: "",
            dianzanNum: 0,
            holeAno: false,
            date: "",
            dianzanUrl: "../images/不赞.png",
            touxiangUrl: "",
            dianzan: false,
            comments: [{
              userid: 10082920,
              avatar: "/images/testImages/sampleHead_2.jpg",
              name: "又一个名字",
              publishDate: "2022-10-10",
              likes: 10,
              text: "样例文本例文本样例文本样例文本样例文文本样例文本样例文本样例文本样例文本样例文本样例文本",
            }, {
              userid: 10082920,
              avatar: "/images/testImages/sampleHead_3.jpg",
              name: "双一个名字",
              publishDate: "2022-10-10",
              likes: 10,
              text: "样例例文本样例文本样例文本",
            }]
          }
          holeData.dianzan = res.data[i].holeIfThumb;
          holeData.holeAno = res.data[i].holeAno;
          holeData.holeID = res.data[i].holeID;
          holeData.text = res.data[i].holeContent;
          holeData.name = res.data[i].holeUserName;
          holeData.dianzanNum = res.data[i].holeThumb;
          holeData.date = res.data[i].holeDate;
          holeData.touxiangUrl = res.data[i].holeUserPictureUrl;
          console.log(holeData);
          holeList.push(holeData);
        }
        console.log(holeList);
        /*
         * 导入数据
         */
        listData = listData.concat(holeList)
        this.setData({
          text: "找到啦！！！",
          holeData: listData,
          navState: 0,
          showHole: true,
        })
      } else {
        this.setData({
          text: "很遗憾，没找到"
        })
        console.log("没有回应");
      }
    }, (err) => {
      //调用服务端登录接口失败
      console.log(err.errMsg)
    })
  },

  search: function () {
    var self = this;
    self.setData({
      text: "查找中...",
      search_text_flag: true,
      showHole: false,
      showHole: false,
      wallData: [],
      holeData: [],
    })
    if (self.data.index == 0) {
      self.wallSearch();
    } else {
      self.holeSearch();
    }
    this.setData({
      textVal: "",
    })

  },
  // 点击下拉显示框
  selectTap() {
    this.setData({
      show: !this.data.show
    });
  },
  // 点击下拉列表
  optionTap(e) {
    let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    this.setData({
      index: Index,
      show: !this.data.show
    });
  },
  close: function () {
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          screenHeight: res.screenHeight,
          swiperHeight: res.screenHeight - res.screenWidth / 3,
        })
      },
    })
  },

})